package com.traveling.hztraveling.controller;


import com.traveling.hztraveling.entity.po.HzRoutes;
import com.traveling.hztraveling.entity.vo.Result;
import com.traveling.hztraveling.mapper.HzRoutesMapper;
import com.traveling.hztraveling.service.IHzRoutesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LHD
 * @since 2025-05-29
 */
@Slf4j
@RestController
@RequestMapping("/routes")
@RequiredArgsConstructor
@Api(tags = "旅游路线相关接口")
public class HzRoutesController {

    private final IHzRoutesService hzRoutesService;

    @PostMapping
    @ApiOperation("添加旅游路线功能")
    public Result addRoutes(@RequestBody HzRoutes hzRoutes){
        int insert = hzRoutesService.addRoutes(hzRoutes);
        if (insert == 1){
            return Result.success(insert);
        }
        else if (insert == -1){
            return Result.fail("同一用户不可重复添加");
        }
        else {
            throw  new RuntimeException("添加失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除旅游路线功能")
    public Result deleteRoutes(@PathVariable("id") Integer id){
        boolean removeById = hzRoutesService.removeById(id);
        if (removeById){
            return Result.success(removeById);
        }
        else {
            throw new RuntimeException("删除失败");
        }
    }

    @PostMapping("/{id}")
    @ApiOperation("修改旅游路线功能")
    public Result updateRoutes(@PathVariable("id") Integer id, @RequestBody HzRoutes hzRoutes){
        hzRoutes.setId(id);
        boolean update = hzRoutesService.updateById(hzRoutes);
        log.info("修改旅游路线信息:{}", hzRoutes);
        if (update){
            return Result.success(update);
        }
        else {
            throw new RuntimeException("修改失败");
        }
    }

}
